package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.brand.MarketBrand;
import com.barbeque.mall.bean.comment.MarketComment;
import com.barbeque.mall.bean.goods.MarketGoods;
import com.barbeque.mall.bean.goods.MarketGoodsAttribute;
import com.barbeque.mall.bean.goods.MarketGoodsProduct;
import com.barbeque.mall.bean.issue.MarketIssue;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.MarketCollect;
import com.barbeque.mall.bean.wx.goods.*;
import com.barbeque.mall.service.wx.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/21 11:43
 */
@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Autowired
    WxGoodsService wxGoodsService;

    @Autowired
    WxSpecificationService wxSpecificationService;

    @Autowired
    WxIssueService wxIssueService;

    @Autowired
    WxCollectService wxCollectService;

    @Autowired
    WxAttributeService wxAttributeService;

    @Autowired
    WxProductService wxProductService;

    @Autowired
    WxCommentService wxCommentService;

    @Autowired
    WxBrandService wxBrandService;

    @Autowired
    WxCategoryService wxCategoryService;


    @RequestMapping("count")
    public BaseRespVo getGoodsCount(){
        List<MarketGoods> allGoods = wxGoodsService.getAllGoods();

        return BaseRespVo.ok(allGoods.size());
    }

    @RequestMapping("detail")
    public BaseRespVo getGoodsDetail(Integer id){
        //查出商品所有的规格
        List<SpecificationsVO> specificationVO = wxSpecificationService.getSpecificationVO(id);
        //查询issue表
        List<MarketIssue> issue = wxIssueService.getIssue();
        //查询collect表，typeid=0的是商品，查询valueid=goodsid
        List<MarketCollect> collectByGoodsId = wxCollectService.getCollectByGoodsId(id);
        //查询comment表，typeid=0的是商品，查询valueid=goodsid
        List<MarketComment> commentByGoodsId = wxCommentService.getCommentByGoodsId(id);

        List<MarketGoodsAttribute> attributeByGoodsId = wxAttributeService.getAttributeByGoodsId(id);

        MarketGoods goodsByGoodsId = wxGoodsService.getGoodsByGoodsId(id);
        //根据商品信息的brandid查询brand信息
        MarketBrand brandById = wxBrandService.getBrandById(goodsByGoodsId.getBrandId());

        List<MarketGoodsProduct> productByGoodsId = wxProductService.getProductByGoodsId(id);


        WxGoodsDetailVO wxGoodsDetailVO = new WxGoodsDetailVO();
        wxGoodsDetailVO.setSpecificationList(specificationVO);
        ArrayList<String> strings = new ArrayList<>();
        strings.add("");
        wxGoodsDetailVO.setGroupon(strings);
        wxGoodsDetailVO.setIssue(issue);
        if (collectByGoodsId.size() == 0){
            wxGoodsDetailVO.setUserHasCollect(0);
        }else {
            wxGoodsDetailVO.setUserHasCollect(1);
        }
        wxGoodsDetailVO.setShareImage("");
        MarketCommentVO marketCommentVO = new MarketCommentVO();
        marketCommentVO.setCount(commentByGoodsId.size());
        marketCommentVO.setComments(commentByGoodsId);
        wxGoodsDetailVO.setComment(marketCommentVO);
        wxGoodsDetailVO.setShare(true);
        wxGoodsDetailVO.setAttribute(attributeByGoodsId);
        wxGoodsDetailVO.setBrand(brandById);
        wxGoodsDetailVO.setProductList(productByGoodsId);
        wxGoodsDetailVO.setInfo(goodsByGoodsId);




        return BaseRespVo.ok(wxGoodsDetailVO);
    }

    @GetMapping("category")
    public BaseRespVo getGoodsCategory(Integer id) {
        WxGoodsCategoryVO wxGoodsCategoryVO = wxCategoryService.getGoodsCategory(id);
        return BaseRespVo.ok(wxGoodsCategoryVO);
    }

    @GetMapping("list")
    public BaseRespVo getGoodsList(WxGoodsListBO wxGoodsListBO) {
        WxGoodsListVO wxGoodsListVO = wxGoodsService.getGoodsLit(wxGoodsListBO);
        return BaseRespVo.ok(wxGoodsListVO);
    }

    @GetMapping("related")
    public BaseRespVo getRelatedGoods(Integer id) {
        CommonData<MarketGoods> data = wxGoodsService.getRelatedGoods(id);
        return BaseRespVo.ok(data);
    }

}
